//
//  MaintenanceViewController.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/9/14.
//

import UIKit

class MaintenanceViewController: BaseViewController {
    var navBarView: MaintenceNavbarView!
    var serviceView: MaintenceServiceView!
    var storeView: MaintenceStoreView!
    var scrollView: UIScrollView!
    var alphaView: MaintenanceGuideView!    //蒙层
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navBar.isHidden = true
        self.view.backgroundColor = Theme.viewBkgGrayColor
        navBarView = MaintenceNavbarView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: NavBarHight))
        navBarView.carNumber = 0
        self.jumpToLogin()
        navBarView.callBack = { [weak self] item in
            switch item {
                case .service:
                    //服务
                    self?.handleNavbarAction(index: 0)
                case .store:
                    //商城
                    self?.handleNavbarAction(index: 1)
                    if self?.storeView.categoryArray.count == 0 {
                        self?.storeView.requestCategoryList()
                    }
                case .shoppingCar:
                    //购物车
                    self?.shoppingBtnAtion()
                case .favory:
                    //收藏
                    self?.collectBtnAntion()
            }
        }
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.serviceView.fetchGetRequest()
        self.fetchGetCarNumber()
    }
    
    ///获取购物车红点
    private func fetchGetCarNumber(){
        var param: [String: String] = [:]
        if UserModule.shared.isLogined ?? false == false {  //未登录属于游客需要这个
            param["cartKey"] = XTManageCenter.getLocalStringDataValue(aKey: XTManageCenter.CartKey)
        }
        XTRequestCenter.shared.sendRequestMappableTargetObject(target: APIManager.myCartListNum(param: param), objectType: ShoppingCarNumResModel.self) { [self] data in
            let resultObj = data as! ShoppingCarNumResModel
            self.navBarView.carNumber = resultObj.data ?? 0
        } error: { err in
            self.navBarView.carNumber = 0
        }
    }
    
    //MARK: - Antion
    private func handleNavbarAction(index: Int){
        let offsetX = CGFloat(index) * kScreenWidth
        UIView.animate(withDuration: 0.24) {
            self.scrollView.contentOffset.x = offsetX
        }
    }
    
    func jumpToLogin(){
        guard let tabbarVC = UIApplication.shared.keyWindow?.rootViewController as? BaseTabbarController else {
            return
        }
        self.view.addSubview(navBarView)
        
        scrollView = UIScrollView.init(frame: CGRect.init(x: 0, y: NavBarHight, width: kScreenWidth, height: kScreenHeight - NavBarHight - TabbarHeight))
        scrollView.backgroundColor = .white
        scrollView.isPagingEnabled = true
        scrollView.isScrollEnabled = false
        scrollView.contentSize = CGSize.init(width: kScreenWidth * 2, height: scrollView.frame.size.height)
        self.view.addSubview(scrollView)
        
        serviceView = MaintenceServiceView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: scrollView.frame.size.height))
        scrollView.addSubview(serviceView)
        
        storeView = MaintenceStoreView.init(frame: CGRect.init(x: kScreenWidth, y: 0, width: kScreenWidth, height: scrollView.frame.size.height))
        scrollView.addSubview(storeView)
        
        
        let flag = XTManageCenter.getLocalStringDataValue(aKey: UtilCenter.kFirstLoadMaintence)
        if flag == "" {
            alphaView = MaintenanceGuideView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight))
            //蒙层，第一次加载app时出现
            tabbarVC.view.addSubview(alphaView)
        }
        
    }
    
    func shoppingBtnAtion(){
        let vc = ShoppingCarViewController()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    func collectBtnAntion(){
        if UserModule.shared.isLogined == false{
            self.isLogin()
            return
        }
        let vc = MyFavoryViewController()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    
    func isLogin(){
        guard let tabbarVC = UIApplication.shared.keyWindow?.rootViewController as? BaseTabbarController else {
            return
        }
        let vc = LoginViewController()
        let loginNavi = BaseNavigationController.init(rootViewController: vc)
        loginNavi.modalPresentationStyle = .fullScreen
        tabbarVC.present(loginNavi, animated: true)
    }
    
    
}




